import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        //从js中获取的某一个localStorage的值，后期可能改变，但js只执行一次
        //通过localStorage.getItem获取adminmenus
        username: window.localStorage.getItem('username') == null ? '' : JSON.parse(window.localStorage.getitem('username' || '[]')),
        adminMenus: []
    },
    mutations: {
        initAdminMenu(state, menus) {
            state.adminMenus = menus
        },
        login(state, data) {
            state.username = data
                //重写localStorage的setitem方法
                //当调用setItem方法设置新值时，会new Event('setItemEvent')
                //通过window监听
            window.localStorage.setItem('username', JSON.stringify(data))
        },
        logout(state) {
            //不能用null，否则将无法判断user里的具体内容
            state.username = ''
                //登出时通过localStorage.removeitem()删除username;全部清空使用window.localStorage.clear()
            window.localStorage.removeItem('username')
            window.adminmenus = []
        }
    },
    actions: {

    },
    modules: {

    }
})